"""
1. 扩大窗口时，只需要更新window
2. 窗口中字符出现重复时候，开始停止扩大窗口
3. 缩小窗口时，只需要更新window
4. 缩减窗口后，更新结果
"""

import collections


class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        window = collections.defaultdict(int)
        
        left, right = 0, 0
        res = 0
        
        while right < len(s):
            c = s[right]
            
            window[c] += 1
            
            right += 1
            
            while window[c] > 1:
                d = s[left]
                
                window[d] -= 1
                
                left += 1
            
            res = max(res, right - left)
        return res